//
//  LogingViewController.m
//  随拍
//
//  Created by lanou3g on 16/1/22.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "LogingViewController.h"
#import "RegisterViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "RootBarViewController.h"

@interface LogingViewController () <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *userName;
@property (strong, nonatomic) IBOutlet UITextField *passWord;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LogingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addTextField:self.userName ImageView:@"1.jpg" placeholder:@"请输入用户名" clearButtonMode:UITextFieldViewModeWhileEditing];
    _userName.delegate = self;
    
    [self addTextField:self.passWord ImageView:@"1.jpg" placeholder:@"请输入密码" clearButtonMode:UITextFieldViewModeWhileEditing];
    _passWord.secureTextEntry = YES;
    _passWord.delegate = self;
}

- (void)addTextField:(UITextField *)textField ImageView:(NSString *)imageView placeholder:(NSString *)placehoder clearButtonMode:(UITextFieldViewMode)clearButton{
    
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageView]];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftImageView;
    textField.placeholder = placehoder;
    textField.delegate = self;
    textField.clearButtonMode = clearButton;
}
- (IBAction)loginAction:(UIButton *)sender {
    [AVUser logInWithUsernameInBackground:self.userName.text password:self.passWord.text block:^(AVUser *user, NSError *error) {
        if (user != nil) {
            [self presentViewController:[RootBarViewController new] animated:YES completion:nil];
        } else {
            UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码错误" preferredStyle:UIAlertControllerStyleAlert];
//            UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//                NSLog(@"取消");
//            }];
            UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"点击了确定按钮");
            }];
//            [alertVc addAction:cancle];
            [alertVc addAction:confirm];
            [self presentViewController:alertVc animated:YES completion:^{
                
                nil;
                
            }];
        }
    }];
    
}

- (IBAction)registerAction:(UIButton *)sender {
    [self showViewController:[RegisterViewController new] sender:nil];
    
}

@end
